package com.example.qu_tao.View;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.qu_tao.R;
import com.example.qu_tao.provider.TQProvider;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView login;
    private EditText usernameInput, phoneInput, emailInput, passwordInput, confirmPasswordInput;
    private Button registerButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        login = findViewById(R.id.login_link);
        usernameInput = findViewById(R.id.username_input);
        phoneInput = findViewById(R.id.phone_input);
        emailInput = findViewById(R.id.email_input);
        passwordInput = findViewById(R.id.password_input);
        confirmPasswordInput = findViewById(R.id.confirm_password_input);
        registerButton = findViewById(R.id.register_button);

        login.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.login_link) {
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
        } else if (id == R.id.register_button) {
            String username = usernameInput.getText().toString();
            String phone = phoneInput.getText().toString();
            String email = emailInput.getText().toString();
            String password = passwordInput.getText().toString();
            String confirmPassword = confirmPasswordInput.getText().toString();

            if (username.isEmpty() || phone.isEmpty() || email.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
                Toast.makeText(this, "所有字段都不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!password.equals(confirmPassword)) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            // 检查用户名是否已存在
            if (isUsernameExists(username)) {
                Toast.makeText(this, "用户名已存在，请选择其他用户名", Toast.LENGTH_SHORT).show();
                return;
            }

            // 插入用户数据到数据库
            ContentValues values = new ContentValues();
            values.put("user_name", username);
            values.put("user_phone", phone);
            values.put("user_email", email);
            values.put("user_password", password);
            Uri uri = Uri.parse("content://com.example.qu_tao.provider/user");
            getContentResolver().insert(uri, values);

            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private boolean isUsernameExists(String username) {
        Uri uri = Uri.parse("content://com.example.qu_tao.provider/user");
        Cursor cursor = getContentResolver().query(uri, null, "user_name = ?", new String[]{username}, null);
        boolean exists = cursor != null && cursor.getCount() > 0;
        if (cursor != null) {
            cursor.close();
        }
        return exists;
    }
}